package com.caocaog.market.service.impl;

import com.caocaog.market.service.IShopBasicService;
import com.caocaog.market.service.ShopGenericService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;

/**
 * 店铺信息通用 Service
 *
 * @author chechen
 * @date 2022-04-18
 */
@Slf4j
@Service
public class ShopGenericServiceImpl implements ShopGenericService {
    private CommonAPI commonAPI;
    private IShopBasicService shopBasicService;

    @Override
    public List<String> getShopIds() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (CommonConstant.USER_TYPE_SYSTEM.equals(loginUser.getUserType())) {
            return new ArrayList<>();
        }
        if (CommonConstant.USER_TYPE_SITE.equals(loginUser.getUserType())) {
            //获取商户ids
            List<String> merchantIds = this.commonAPI.findMerchantForSite(loginUser.getSiteId());
            if (merchantIds == null || merchantIds.isEmpty()) {
                throw new JeecgBootException("还没有商家，请先添加商家，再添加店铺");
            }
            log.info("根据商家id集合获取店铺id集合");
            List<String> shopIds = this.shopBasicService.getShopIdsByMerchantIds(merchantIds);
            if (shopIds.isEmpty()) return null;
            return shopIds;
        }
        if (CommonConstant.USER_TYPE_MERCHANT.equals(loginUser.getUserType())) {
            //商户
            String merchantId = loginUser.getMerchantId();

            if (StringUtils.isNotBlank(loginUser.getCurrentSelectedShopId()))
                return Collections.singletonList(loginUser.getCurrentSelectedShopId());

            List<String> shopIds = this.shopBasicService.getShopIdsByMerchantIds(Collections.singletonList(merchantId));
            if (shopIds.isEmpty()) return null;
            return shopIds;
        }
        if (loginUser.getUserType()!=null)
            return CommonConstant.EMPTY_STR_LIST;
        throw new JeecgBootException("LoginUser Type Error");
    }

    @Override
    public <E> void dealWithShopId(BiFunction<E, String, E> set, E entity) {
        List<String> shopIds = this.getShopIds();
        if (shopIds == null)
            throw new JeecgBootException("未找到门店");
        if (shopIds.isEmpty())
            shopIds.add(CommonConstant.DEFAULT_SHOP_ID);
        set.apply(entity, shopIds.get(0));
    }

    @Autowired
    public void setCommonAPI(CommonAPI commonAPI) {
        this.commonAPI = commonAPI;
    }

    @Autowired
    public void setShopBasicService(IShopBasicService shopBasicService) {
        this.shopBasicService = shopBasicService;
    }
}
